const utils = require('./utils/utils.js');
const electron = require('electron');
const app = electron.app;
const dialog = electron.dialog;

var messageBox = (title, message) => {
    message = message || "message";
    dialog.showMessageBox(app.mainWindow, {
        type: 'info',
        title,
        message,
        buttons: ['ok', 'cancel']
    }, (index) => {
        if (index == 0) {
            console.log('You click ok.');
        } else {
            console.log('You click cancel.');
        }
    })
};

var remoteCvSrv = null;
var menu = [
    {
        label: '文件',
        submenu: [
            {
                label: '本机IP',
                click: function () {
                    messageBox(utils.getHostname() + ":ips", utils.getIps());
                }
            },
            {
                label: '本机TCP端口',
                click: function () {
                    utils.getTcpPorts((str) => {
                        messageBox(utils.getHostname() + ":tcp-ports(netstat -ano)", str);
                    });                   
                }
            },
            {
                label: '本机进程列表',
                click: function () {
                    utils.getTasklist((str) => {
                        messageBox(utils.getHostname() + ":tasklist(tasklist)", str);
                    });                   
                }
            },
            {
                type: 'separator'
            },
            {
                label: '退出',
                click: function () {
                    app.forceExit();
                }
            }
        ]
    },
    {
        label: '工具',
        submenu: [
            {
                label: 'remoteCV',
                click: function () {
                    if (!remoteCvSrv) {
                        remoteCvSrv = utils.remoteCV();
                    }
                    electron.shell.openExternal('http://localhost:2313/');
                }
            },
            {
                label: 'ping.pe',
                click: function () {
                    electron.shell.openExternal('http://ping.pe/');
                }
            },
            {
                label: 'tcp.port',
                click: function () {
                    electron.shell.openExternal('http://port.ping.pe/');
                }
            }
        ]
    },
    {
        label: '帮助',
        submenu: [
            {
                label: 'http',
                click: function () {
                    messageBox("http");
                }
            },
            {
                label: 'ftp',
                click: function () {
                    messageBox("ftp");
                }
            }
        ]
    },
    {
        label: '关于',
        submenu: [
            {
                label: 'electron',
                click: function () {
                    electron.shell.openExternal('http://electron.atom.io');
                }
            },
            {
                label: 'gitee',
                click: function () {
                    electron.shell.openExternal('https://gitee.com/littlepro/easySrv');
                }
            },
            {
                label: 'me',
                click: function () {
                    messageBox("me");
                }
            }
        ]
    }
];

var trayMenu = [
    {
        label: '展示',
        click: function () {
            app.showWindow();
        }
    },
    {
        label: '隐藏',
        click: function () {
            app.hideWindow();
        }
    },
    {
        label: '退出',
        click: function () {
            app.forceExit();
        }
    }
];

var contextMenu = [
    {
        label: 'Hello'
    },
    {
        type: 'separator'
    },
    {
        label: 'Electron',
        type: 'checkbox',
        checked: true
    }
];

module.exports = {
    menu,
    trayMenu,
    contextMenu
};